package org.kl.bf.web.basic;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.kl.bf.entity.basic.NotifyGroup;
import org.kl.bf.entity.easyui.EasyuiUtils;
import org.kl.bf.service.basic.NotifyGroupService;
import org.kl.bf.utils.KLServlets;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.MediaTypes;

@Controller
@RequestMapping(value = "/basic/notifyGroup")
public class NotifyGroupController extends BasicController {
	@Autowired
	private NotifyGroupService notifyGroupService;

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("userName", BasicController.getCurrentUserName());
		model.addAttribute("userId", BasicController.getCurrentUserId());
		return "basic/notifyGroupList";
	}

	@RequestMapping(value = "dataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String dataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		String dataDetailMap = BasicController.getCurrentUserName();
		searchParams.put("IN_user.trueName", dataDetailMap);
		Page<NotifyGroup> notifyGroup = notifyGroupService.getNotifyGroup(searchParams, pageNumber, pageSize, sortType, orderType);
		return EasyuiUtils.buildEasyuiGridJsonString(notifyGroup.getContent(), (int) notifyGroup.getTotalElements());
	}

	@RequestMapping(value = "update", method = RequestMethod.POST, produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String update(@Valid @ModelAttribute("notifyGroup") NotifyGroup notifyGroup, BindingResult result, HttpSession session) {
		if (result.hasErrors()) {
			return ajaxMsg(bindingMsg(result), false, null);
		}
		try {
			notifyGroupService.saveNotifyGroup(notifyGroup);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("编辑失败[" + e.getMessage() + "]", false, null);
		}
		List<NotifyGroup> notifyGroupsList = notifyGroupService.getAllNotifyGroups();
		for (NotifyGroup c : notifyGroupsList) {
			session.getServletContext().setAttribute(c.getId(), c.getGroupName());
		}
		return ajaxMsg("编辑成功", true, null);
	}

	@RequestMapping(value = "delete/{id}", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes, HttpSession session) {
		notifyGroupService.deleteNotifyGroup(id);
		List<NotifyGroup> notifyGroupsList = notifyGroupService.getAllNotifyGroups();
		for (NotifyGroup c : notifyGroupsList) {
			session.getServletContext().setAttribute(c.getId(), c.getGroupName());
		}
		return ajaxMsg("删除成功", true, null);
	}

	@RequestMapping(value = "multiDelete", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String multiDelete(@RequestParam(value = "ids", defaultValue = "") String ids) {
		String msg = "";
		try {
			msg = notifyGroupService.multiDelete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("删除失败:" + e.getMessage(), false, null);
		}
		return ajaxMsg("删除成功:" + msg, true, null);
	}

	@ModelAttribute
	public void getNotifyGroup(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("notifyGroup", notifyGroupService.getNotifyGroup(id));
		}
	}
}
